package com.hyj.util;
import java.security.Key;
import java.security.KeyFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

import com.sun.org.apache.xml.internal.security.utils.Base64;

public class RSACoderUtil {
	public static final String ALGORITHM = "RSA";
	
	/**
	 * 用公钥加密
	 * @author 胡亚军 2013-08-06
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] encryptByPublicKey(byte[] data, byte[] keyBytes)
			throws Exception {

		// 取得公钥
		X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
		Key publicKey = keyFactory.generatePublic(x509KeySpec);

		// 对数据加密
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.ENCRYPT_MODE, publicKey);

		return cipher.doFinal(data);
	}
	
	
	/**
	 * 用私钥解密
	 * @author 胡亚军2013-08-06
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] decryptByPrivateKey(byte[] data, byte[] keyBytes)
			throws Exception {
		// 对密钥解密

		// 取得私钥
		PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
		Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

		// 对数据解密
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.DECRYPT_MODE, privateKey);

		return cipher.doFinal(data);
	}
	
	/**
	 * 自己的私钥 用来解密  
	 * 公钥已经上传给支付宝
	 * @return
	 */
	public static byte[] getPrivateKey(){
		String privateKey ="MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJqJvyujUSKt8qV9j5qv" +
        		"01lSsds+GZ9f/Kdfo6zoXVHhUU4HIpNgpsFqort+GS2ymVMdh/ED5pM5lMM5Rs5c1DpQPJ201SZyiKKLDN" +
        		"6CGyJYJmb7V8I68vZ6f9iaAw03nV6uZYabA1RPlqA5tRw1viM+IQboYyGzMwCKPaEFpcITAgMBAAECgYBgyQ" +
        		"ZuuSE2fATs+n923MmcNrtnWTRI6Z/d33st7nWLs3btTHCuJqkpl7SFgosYYLPAJ02dd9jvue9eeT/MPL05MP" +
        		"vbMGTlZVibAE4eIYF/plY7zHnXYt9vvFjSOkCTItNZANb+VylX3lmvGw0PLbZqKI/KgJ5m6IdJIrH8M5mBAQJB" +
        		"AOLfsN+cUKx6PF33nnVr542MZh1A4t8N2tUS42cuM3ym4yrH3ICRr7A4Hthpqox5no+p+mFJku7cBbRp7MuJ" +
        		"SoECQQCuYLW+4vbtp8KMJ1qI0RPCzLwZfLF8uMCi68wYjLqfpdHXDiR1/lA+STJe5HTOXKmpQeV2Zcig6X2K" +
        		"BWIADvqTAkEAkWXMxDI43aDlKU+1pQlHd8bf9qWMAGmvsliNlqXcZ2nJMrSt01QK5YPB+O4XWDnlbasBBPKTE" +
        		"TO/EDXGhVbygQJBAKJfWTz7GJVpbgWVq37UuMLeY5SUAK0zV+UdDfoaKLj57fCDsb3RZiTpgabMsltp+22o96" +
        		"MM2gnEAWrg4kVNrVMCQExshPMDGkKF/bKJhyNpPW/Rwr91v11IKn4n+mzvkRNxBQ/YFBHi/Rgfu0ZNbeKsFK" +
        		"R23/iyHzU+hLLZ/6JMO2Q=";
		try {
			return decryptBASE64(privateKey);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 获取 支付宝的公钥 用来加密 
	 * @return
	 */
	public static byte[] getPublicKey(){
		String publicKey ="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6" +
				"BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAop" +
				"rih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB";
		try {
			return decryptBASE64(publicKey);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * BASE64解密
	 * 
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] decryptBASE64(String key) throws Exception {
		return	Base64.decode(key);
		//  return (new BASE64Decoder()).decodeBuffer(key);
	}
	
	
	/**
	 * 商户自己的 密钥对
	 * @param arts
	 * @throws Exception
	 */
	public static void  main(String... arts ) throws Exception{
		
		String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCaib8ro1EirfKlfY+ar9NZUrHbPh" +
				"mfX/ynX6Os6F1R4VFOByKTYKbBaqK7fhktsplTHYfxA+aTOZTDOUbOXNQ6UDydtNUmcoiiiwzeghsiWCZm+1" +
				"fCOvL2en/YmgMNN51ermWGmwNUT5agObUcNb4jPiEG6GMhszMAij2hBaXCEwIDAQAB";

        String privateKey ="MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJqJvyujUSKt8qV9j5qv" +
        		"01lSsds+GZ9f/Kdfo6zoXVHhUU4HIpNgpsFqort+GS2ymVMdh/ED5pM5lMM5Rs5c1DpQPJ201SZyiKKLDN" +
        		"6CGyJYJmb7V8I68vZ6f9iaAw03nV6uZYabA1RPlqA5tRw1viM+IQboYyGzMwCKPaEFpcITAgMBAAECgYBgyQ" +
        		"ZuuSE2fATs+n923MmcNrtnWTRI6Z/d33st7nWLs3btTHCuJqkpl7SFgosYYLPAJ02dd9jvue9eeT/MPL05MP" +
        		"vbMGTlZVibAE4eIYF/plY7zHnXYt9vvFjSOkCTItNZANb+VylX3lmvGw0PLbZqKI/KgJ5m6IdJIrH8M5mBAQJB" +
        		"AOLfsN+cUKx6PF33nnVr542MZh1A4t8N2tUS42cuM3ym4yrH3ICRr7A4Hthpqox5no+p+mFJku7cBbRp7MuJ" +
        		"SoECQQCuYLW+4vbtp8KMJ1qI0RPCzLwZfLF8uMCi68wYjLqfpdHXDiR1/lA+STJe5HTOXKmpQeV2Zcig6X2K" +
        		"BWIADvqTAkEAkWXMxDI43aDlKU+1pQlHd8bf9qWMAGmvsliNlqXcZ2nJMrSt01QK5YPB+O4XWDnlbasBBPKTE" +
        		"TO/EDXGhVbygQJBAKJfWTz7GJVpbgWVq37UuMLeY5SUAK0zV+UdDfoaKLj57fCDsb3RZiTpgabMsltp+22o96" +
        		"MM2gnEAWrg4kVNrVMCQExshPMDGkKF/bKJhyNpPW/Rwr91v11IKn4n+mzvkRNxBQ/YFBHi/Rgfu0ZNbeKsFK" +
        		"R23/iyHzU+hLLZ/6JMO2Q=";
		
		// 对公钥解密
		String inpStr = "hyj中国你妈逼啊";
		byte[] data = inpStr.getBytes();



		byte[] encodedData =  encryptByPublicKey(data, decryptBASE64(publicKey));
		byte[] decodedData =  decryptByPrivateKey(encodedData,decryptBASE64(privateKey));
		String outputStr = new String(decodedData);
		System.out.println("加密前: " + inpStr + "\n\r" + "解密后: " + outputStr);
	}
}
